package com.cupk.service.impl;

import com.cupk.mapper.StageMapper;
import com.cupk.mapper.StudentMapper;
import com.cupk.mapper.TopicMapper;
import com.cupk.pojo.Topic;
import com.cupk.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Topic> getTopicByStageId(Integer stageId) {
        return topicMapper.getTopicByStageId(stageId);
    }

    @Override
    public Integer findMaxQuestion(Integer stageId) {
        Integer res = topicMapper.findMaxQuestion(stageId);
        if (res == null) {
            res = 1;
        }
        return res;
    }

    @Override
    public void addTopic(Topic topic) {
        topicMapper.addTopic(topic);
    }

    @Override
    public Topic getTopicById(Integer topicId) {
        return topicMapper.getTopicById(topicId);
    }

    @Override
    public void updateTopic(Topic topic) {
//        Integer questionType = topic.getQuestionType();
//        List<String> questionList = topic.getQuestionChoiceList();
//        if (questionType == Topic.SINGLE_CHOICE || questionType == Topic.MULTIPLE_CHOICE) {
//            String res = questionList.stream().collect(Collectors.joining("\\|"));
//            topic.setQuestionChoice(res);
//        }
        topicMapper.updateTopic(topic);
    }

    @Override
    public Topic transferChoice(Topic l) {
        if (l.getQuestionType() == Topic.SINGLE_CHOICE || l.getQuestionType() == Topic.MULTIPLE_CHOICE) {
            String questionChoice = l.getQuestionChoice();
            List<String> questionChoiceList = Arrays.asList(questionChoice.split("\\|"));
            l.setQuestionChoiceList(questionChoiceList);
        }
        return l;
    }

    @Override
    public void deleteTopic(Integer TopicId) {
        topicMapper.deleteTopic(TopicId);
    }

    @Override
    public List<String> getTopicQuestion(Integer stageId) {
        return topicMapper.getTopicQuestion(stageId);
    }
}
